package com.liusaidh.controller;
import com.liusaidh.domain.Label;
import com.liusaidh.domain.PageBean;
import com.liusaidh.domain.ReportCopy;
import com.liusaidh.domain.User;
import com.liusaidh.service.BlogService;
import com.liusaidh.service.UserService;
import io.swagger.annotations.*;
import javafx.stage.Stage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@Api(tags = "管理员对后台的用户和博文管理")
public class ManagerController {
    @Resource
    private UserService userService;
    @Resource
    private BlogService blogService;


    @ApiOperation(value = "管理员退出", notes = "管理员退出，跳到登录界面")
    @GetMapping(value = "/manager/managerLogout")
    public ModelAndView managerLogout(HttpServletRequest request) {
        request.getSession().removeAttribute("username");
        request.getSession().removeAttribute("id");
        ModelAndView mav = new ModelAndView("redirect:/blog/getIndexBlog?state=1");
        return mav;
    }


    @ApiOperation(value = "分页查询用户",
            notes = "根据条件分页查询用户，管理员根据性别、用户名,id值 查询用户，或者不输入任何的信息，查询所有分页显示")
    @ApiResponses(@ApiResponse(code = 200, message = "pageBean对象的rows属性放数据，total属性是总的记录数", response = PageBean.class))
    @GetMapping("/user/userList")
    public ModelAndView getAllUser(@ApiParam(name = "pageNo", value = "当前的页码数（及当前是第几页，前端传参）,默认展示第一页") @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @ApiParam(name = "pageSize", value = "每页的数量（及每页展示几条数据，前端传参），默认是十条") @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   @ApiParam(name = "condition", value = "根据用户的用户名/性别/id查询用户") @RequestParam(name = "condition", required = false) String condition,
                                   HttpSession session) {
//
//        @ApiParam(value = "根据用户的名字分页查询(男/女)") @RequestParam(value = "username", required = false) String username,
//        @ApiParam(value = "根据用户的性别分页查询") @RequestParam(value = "sex", required = false) Character sex,
//        @ApiParam(value = "根据用户的id查询") @RequestParam(value = "id", required = false) Integer id
        ModelAndView mav = new ModelAndView("backstage");
        mav.addObject("username",session.getAttribute("username"));
        PageBean pageBean = userService.getAllUser(pageNo, pageSize, condition);
        mav.addObject("pageBean", pageBean);
        return mav;
    }

    @ApiOperation(value = "删除用户", notes = "前端传来用户的id，根据id删除用户，只需要把用户的exist值设置为1即可（1默认为不存在，0默认为存在）",response = String.class)
    @GetMapping(value = "/user/deleteUser/{id}")
    @ResponseBody
    public String deleteUserById(@ApiParam(name = "id", value = "前端传来要删除用户的id") @PathVariable("id") Integer id) {
        if (userService.deleteUserById(id) == 1) {
            return "删除成功";
        } else {
            return "删除失败";
        }
    }

    @ApiOperation(value = "添加用户", notes = "管理员添加对应的用户")
    @PostMapping("/user/addUser")
    public ModelAndView addUser(@ApiParam(name = "username", value = "用户的昵称") @RequestParam(name = "username",required = false) String username,
                                @ApiParam(name = "email", value = "用户的邮箱",required = true) @RequestParam(name = "email") String email,
                                @ApiParam(name = "sex", value = "用户的性别") @RequestParam(name = "sex",required = false,defaultValue = "男") Character sex,
                                @ApiParam(name = "manager", value = "是不是管理员，0不是，1是") @RequestParam(name = "manager",required = false,defaultValue = "0") Integer manager,
                                @ApiParam(name = "age", value = "用户的年龄") @RequestParam(name = "age",required = false) Integer age,
                                @ApiParam(name = "password", value = "用户的密码",required = true) @RequestParam(name = "password") String password,
                                @ApiParam(name = "address", value = "用户的地址") @RequestParam(name = "address",required = false) String address
    ) {
        long totalCount = userService.getTotalCount(null);
        ModelAndView mav = new ModelAndView("redirect:/user/userList?pageNo=" + ((totalCount/10)+1));
        if (userService.addUser(new User(email, username, password, sex, age, manager, address)) == 1) {
            mav.addObject("msg", "添加成功！");
        } else {
            mav.addObject("msg", "添加失败");
        }
        return mav;
    }


    @ApiOperation(value = "修改用户的信息", notes = "修改用户的信息",response = String.class)
    @GetMapping(value = "/user/updateUser")
    @ResponseBody
    public String updateUser(@ApiParam(name = "id", value = "用户的id",required = true) @RequestParam(name = "id") Integer id,
                                   @ApiParam(name = "username", value = "用户的昵称") @RequestParam(name = "username",required = false) String username,
                                   @ApiParam(name = "email", value = "用户的邮箱") @RequestParam(name = "email",required = false) String email,
                                   @ApiParam(name = "sex", value = "用户的性别") @RequestParam(name = "sex",required = false) Character sex,
                                   @ApiParam(name = "manager", value = "是不是管理员，0不是，1是") @RequestParam(name = "manager",required = false) Integer manager,
                                    @ApiParam(name = "age", value = "用户的年龄") @RequestParam(name = "age",required = false) Integer age,
                                   @ApiParam(name = "address", value = "用户的地址") @RequestParam(name = "address",required = false) String address) {
     return userService.updateUser(new User(id, email, username, sex, age, manager, address)) == 1 ?  "修改成功":"修改失败";
    }

    @ApiOperation(value = "禁言用户", notes = "将用户禁言",response = String.class)
    @GetMapping(value = "/user/banUserToComment/{id}")
    @ResponseBody
    public String banUserToComment(@ApiParam(name = "id", value = "前端传来当前的用户的id") @PathVariable("id") Integer id) {
       return userService.banUserToComment(id) == 1 ? "禁言成功" : "禁言失败";
    }

    @ApiOperation(value = "取消禁言用户", notes = "将用户禁言",response = String.class)
    @GetMapping(value = "/user/notBanUserToComment/{id}")
    @ResponseBody
    public String notBanUserToComment(@ApiParam(name = "id", value = "前端传来当前的用户的id") @PathVariable("id") Integer id) {
        return userService.notBanUserToComment(id) == 1 ? "取消禁言成功":"取消禁言失败";
    }

    //  ------------------------------------管理文章-------------------------------------
    @ApiOperation(value = "查询待审核的文章", notes = "查询出待审的文章，让管理员审核，每页展示一条，页码由前端传参")
    @ApiResponses(@ApiResponse(code = 200, message = "查询成功,返回一个pageBean对象，rows属性放的是一个个AuditBlog对象，total属性放的是总的记录数", response = PageBean.class))
    @GetMapping("/blog/getAuditBlogList")
    public ModelAndView getAuditBlogList(@ApiParam(name = "pageNo", value = "当前的页码数（及当前是第几页，前端传参）,默认展示第一页") @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,HttpSession session) {
        ModelAndView mav = new ModelAndView("backstage-blogCheck");
        mav.addObject("username",userService.getUserById((Integer)session.getAttribute("id") ).getUsername());
        PageBean pageBean = blogService.getAuditBlogList(pageNo, 1);
        mav.addObject("pageBean", pageBean);
        return mav;
    }

    @ApiOperation(value = "通过审核", notes = "管理员点击通过审核")
    @GetMapping("/blog/passTheAudit")
    public ModelAndView passTheAudit(@ApiParam(name = "id", value = "文章的id值") @RequestParam(name = "id") Integer id) {
        ModelAndView mav = new ModelAndView("redirect:/blog/getAuditBlogList");
        if (blogService.passTheAudit(id) == 1){
            mav.addObject("msg","操作成功");
        }else{
            mav.addObject("msg","操作失败");
        }
        return mav;
    }

    @ApiOperation(value = "不通过审核", notes = "管理员点击不通过审核")
    @GetMapping("/blog/notPassTheAudit")
    public ModelAndView notPassTheAudit(@ApiParam(name = "id", value = "文章的id值") @RequestParam(name = "id") Integer id) {
        ModelAndView mav = new ModelAndView("redirect:/blog/getAuditBlogList");
        if (blogService.notPassTheAudit(id) == 1){
            mav.addObject("msg","操作成功");
        }else{
            mav.addObject("msg","操作失败");
        }
        return mav;
    }
    //  ------------------------------------管理热搜（标签）------------------------------------

    @GetMapping("/manager/getHotSearchBlog")
    @ApiOperation(value = "管理员管理热搜", notes = "pageBean中放的有Label对象，和总的记录数",response = Label.class)
    public ModelAndView getHotSearchBlog(@ApiParam(name = "pageNo", value = "当前的页码数（及当前是第几页，前端传参）,默认展示第一页") @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                         @ApiParam(name = "pageSize", value = "每页的数量（及每页展示几条数据，前端传参），默认是十条") @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                         HttpSession session) {
        ModelAndView mav = new ModelAndView("backstage-blogManage");
        mav.addObject("username",userService.getUserById((Integer)session.getAttribute("id") ).getUsername());
        PageBean pageBean = blogService.getLabelForManager(pageNo, pageSize);
        mav.addObject("pageBean", pageBean);
        return mav;
    }

    @GetMapping("/manager/deleteHotSearchBlog")
    @ApiOperation(value = "管理员删除热搜", notes = "管理员可以删除一些不好的热搜")
    @ResponseBody
    public String deleteHotSearchBlog(@ApiParam(name = "id", value = "标签的id") @RequestParam("id") Integer id) {
        return blogService.deleteHotLabel(id) > 0 ? "删除成功":"删除失败";
    }

    @GetMapping("/manager/typeList")
    @ApiOperation(value = "管理员管理类别/或模糊查询类别", notes = "获取全部的类别或根据条件搜索，返回pageBean对象", response = PageBean.class)
    public ModelAndView getAllTypeForManager(@ApiParam(name = "pageNo", value = "当前的页码数（及当前是第几页，前端传参）,默认展示第一页") @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                             @ApiParam(name = "pageSize", value = "每页的数量（及每页展示几条数据，前端传参），默认是十条") @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                             @ApiParam(name = "type", value = "类别") @RequestParam(name = "type", required = false) String type,
                                             HttpSession session) {
        ModelAndView mav = new ModelAndView("backstage-cateManage");
        mav.addObject("username",userService.getUserById((Integer)session.getAttribute("id") ).getUsername());
        PageBean pageBean = blogService.getAllTypeForManager(pageNo, pageSize, type);
        mav.addObject("pageBean", pageBean);
        return mav;
    }

    @GetMapping("/manager/deleteType/{id}")
    @ApiOperation(value = "管理员删除类别，前端传来一个类别的id", notes = "管理员可以对类别进行删除")
    @ResponseBody
    public String deleteType(@ApiParam(name = "id", value = "要删除的类别的id值") @PathVariable("id") Integer id) {
      return blogService.deleteType(id)>0 ? "删除成功":"删除失败";
    }


    @GetMapping("/manager/addType")
    @ApiOperation(value = "管理员添加类别", notes = "管理员可以添加类别")
    public ModelAndView addType(@ApiParam(name = "type", value = "类别") @RequestParam("type") String type) {
        ModelAndView mav = new ModelAndView("redirect:/manager/typeList?pageNo=" + ((blogService.getAllTypeCount()/10) + 1));
       if (blogService.addType(type) == 1){
           mav.addObject("msg","添加成功");
       }else {
           mav.addObject("msg","添加失败");
       }
        return mav;
    }

    //========================================管理举报========================================================

    @ApiOperation(value = "获取有关举报的信息给管理员",notes = "信息在pageBean中",response = ReportCopy.class)
    @GetMapping("/manager/getReportForManager")
    public  ModelAndView getReportForManager(@ApiParam(name = "pageNo", value = "当前的页码数（及当前是第几页，前端传参）,默认展示第一页") @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                             @ApiParam(name = "pageSize", value = "每页的数量（及每页展示几条数据，前端传参），默认是十条") @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                             HttpSession session){
        ModelAndView mav = new ModelAndView("backstage-reportHandle");
        mav.addObject("username",userService.getUserById((Integer)session.getAttribute("id") ).getUsername());
        PageBean pageBean =  blogService.getReportForManager(pageNo,pageSize);
        mav.addObject("pageBean",pageBean);
        return mav;
    }

    @ApiOperation(value = "通过举报",notes = "管理员通过用户对文章的举报",response = String.class)
    @GetMapping("/manager/passReport")
    @ResponseBody
    public  String passReport(@ApiParam(name = "id",value = "文章的id") @RequestParam("id") Integer id){
        return blogService.passReport(id) > 0 ? "成功" : "失败";
    }

    @ApiOperation(value = "不通过举报",notes = "管理员不通过用户对文章的举报",response = String.class)
    @GetMapping("/manager/notPassReport")
    @ResponseBody
    public  String notPassReport(@ApiParam(name = "id",value = "文章的id") @RequestParam("id") Integer id){
        return blogService.notPassReport(id) > 0 ? "成功" : "失败";
    }

}
